package cn.syc.struck.demo.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

import cn.syc.struck.demo.R;
import cn.syc.struck.demo.adapter.MyAdapter;
import cn.syc.struck.demo.base.BaseFragment;
import cn.syc.struck.demo.http.entity.gen.BlogPager;
import cn.syc.struck.demo.http.service.BlogService;
import rx.Subscriber;

/**
 * Created by chenzz on 2017/3/23.
 */

public class LinearFragment extends BaseFragment {
    public static final String CLASS_TAG = "LinearFragment";

    private XRecyclerView mRecyclerView;
    private MyAdapter mAdapter;
    private ArrayList<String> listData;
    private View mEmptyView;

    private TextView textView_text;

    //当前页码
    int curpage = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recyclerview;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {

        mRecyclerView = (XRecyclerView) mRootView.findViewById(R.id.recyclerview);
        textView_text = (TextView) mRootView.findViewById(R.id.textView_text);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        mRecyclerView.setLayoutManager(layoutManager);

        //----------add2-------
        mEmptyView = mRootView.findViewById(R.id.include_view_empty);
        mRecyclerView.setEmptyView(mEmptyView);
        //-----------2---------

        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);

        //View header = LayoutInflater.from(getActivity()).inflate(R.layout.recyclerview_header, (ViewGroup) mRootView.findViewById(android.R.id.content), false);
        //mRecyclerView.addHeaderView(header);

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                textView_text.setText("正在加载...");
                curpage = 1;
                BlogService.getInstance().getBlogs(curpage, new PagerSubscriber());
            }

            @Override
            public void onLoadMore() {
                //++curpage,加之后再用值，则为第二页开始
                BlogService.getInstance().getBlogs(++curpage, new PagerSubscriber());

            }
        });

        listData = new ArrayList<String>();
        mAdapter = new MyAdapter(listData);

        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.refresh();

        //========================
        mRootView.findViewById(R.id.button_reload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //相当于重新下拉刷新
                mRecyclerView.refresh();
                mRecyclerView.refreshComplete();
            }
        });
        //--------------------------
    }


    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {@Tag(CLASS_TAG)}
    )
    public void onHttpError(String txt) {
        //Toast.makeText(LinearActivity.this, "出错:代码为:" + code, Toast.LENGTH_SHORT).show();
        textView_text.setText(txt);
    }

    //-------------------
    private class PagerSubscriber extends Subscriber<BlogPager> {
        private String tag;

        public PagerSubscriber() {
            this("tag");
        }

        public PagerSubscriber(String tag) {
            this.tag = tag;
        }


        @Override
        public void onCompleted() {
            Toast.makeText(getActivity(), "completed!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Throwable e) {
            //mPullDownSRL.setRefreshing(false);
            if (curpage > 1)
                mRecyclerView.loadMoreComplete();
            else
                mRecyclerView.refreshComplete();

            if (listData.size() > 0) {
                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
            } else {
                textView_text.setText(e.getMessage());
            }
        }

        @Override
        public void onNext(BlogPager t) {
            if (curpage == 1)
                listData.clear();

            if (t.getTotalRow() > 0) {
                for (BlogPager.ListBean b : t.getList()) {
                    String txt = String.format("%02d.%s\r\n%s", b.getId(), b.getTitle(), b.getContent());
                    listData.add(txt);
                    //在加载更多时调用下面方法
                    //mAdapter.notifyItemInserted(mAdapter.getItemCount());
                }
                //---------------------------
                mAdapter.notifyDataSetChanged();
                //---------------------------
                if (curpage > 1) {
                    // 加载结束务必调用方法refreshComplete,loadMoreComplete,setNoMore
                    // 将XRecyclerView中变量isLoadingData设为false结束加载状态
                    mRecyclerView.loadMoreComplete();
                    if (t.isLastPage())
                        mRecyclerView.setNoMore(true);
                } else {
                    mRecyclerView.refreshComplete();
                    if (t.getTotalPage() <= 1)
                        mRecyclerView.setNoMore(true);
                }
            } else {
                textView_text.setText("没有数据");
                mAdapter.notifyDataSetChanged();
            }
        }
    }
    //-------------------

}
